<template>
  <div>
    <el-table
      ref="commitTable"
      v-loading="loading"
      :data="commits"
      border
      stripe
      height="700"
      row-key="commitId"
      size="mini"
      @selection-change="selectionChange"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="author"
        label="作者"
        width="180"
      />
      <el-table-column
        prop="message"
        label="提交信息"
      />
      <el-table-column
        prop="time"
        label="提交时间"
        width="150"
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="100"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" :disabled="scope.row.commitId === commits[0].commitId" @click="switchBranch(scope.row.commitId)">切换</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>

import vcs from '@/api/vcs'

export default {
  name: 'commit',
  props: {
    group: String,
    repository: String
  },
  data(){
    return {
      commits:[],
      loading: false
    }
  },
  methods: {
    switchBranch(commitId){
      this.$emit('switchBranch',commitId)
    },
    selectionChange(selection){
      this.$emit('selection-change',selection)
    },
    loadCommits(){
      this.loading = true
      if (this.group && this.repository){
        vcs.git.commits(this.group,this.repository).then(res => {
          this.loading = false
          this.commits = res.data
        }).catch(res => {
          this.loading = false
        })
      }
    }
  },
  watch: {
    repository:{
      handler(){
        this.loadCommits()
      }
    }
  }
}
</script>

<style scoped>

</style>
